package com.buddy.learn.javasource._lock;

import java.util.concurrent.Semaphore;

/**
 * @author Buddy
 * @date 2021/5/15
 */
public class _Semaphore {


    public static void main(String[] args) {
        Semaphore semaphoreA = new Semaphore(1);
        Semaphore semaphoreB = new Semaphore(0);
        Semaphore semaphoreC = new Semaphore(0);

        new Thread(() -> {
            for (int i=0;i<10;i++) {
                try {
                    semaphoreA.acquire();
                    System.out.println(semaphoreA.availablePermits());
                    System.out.println("A");
                    semaphoreB.release();
                    System.out.println(semaphoreB.availablePermits());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }).start();
        new Thread(() -> {
            for (int i=0;i<10;i++) {
                try {
                    semaphoreB.acquire();
                    System.out.println("B");
                    semaphoreC.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }).start();
        new Thread(() -> {
            for (int i=0;i<10;i++) {
                try {
                    semaphoreC.acquire();
                    System.out.println("C");
                    semaphoreA.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }

}
